// pages/square/square.ts

import axios from "../../utils/axios"


Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbarindex:1, 
    tabbarlist:["店铺","广场","其他"],
    squaretabbarlist:["最近","最新","比赛组队","分类4"],
    squaretabbarindex:0,
    squarepage:0,
    squarelast:false,
    dynamics:[],
    dynamiccommentid:0,
    dynamiccomments:[],
    ondown:false,
    squaresearchkey:"",
    show:false,
    shoptabbarlist:["推荐","娱乐","丽人","生活"],
    shoptabbarindex:0,
    shoppage:0,
    shoplast:false,
    schoolregion:["大学城","龙洞","番禺"],
    regionindex:0,
    shopondown:false,
    shops:[],
    shopsearchkey:"",
    dynamicid:0,
    parentid:0,
  },
  async getdynamic(searchkey){
    var url="/dynamics?personal=false&type="+this.data.squaretabbarlist[this.data.squaretabbarindex]+"&page="+this.data.squarepage+"&size=5"
    if(searchkey!=null){
      url+="&tag="+searchkey
    }
    var result=await axios.get(url)
    console.log(result)
    var dynamicslist=this.data.dynamics
    for (let i = 0; i < result.content.length; i++) {
      const item = result.content[i];
      var dynamics={id:item[0],
        content:item[1],
        avatar:item[11],
        anonymous:item[4],
        provided:item[5],
        contact:item[9],
        name:item[10],
        commentnumber:item[13],
        lovenumber:item[6],
        sex:item[12],
        islove:item[14],
      userid:item[8]}
      var tags=item[3].split(",")
      dynamics.tags=tags
    dynamics.src=item[2].split(",")
    var datetime=new Date(item[7])
    var datetimestr=""
    var period=new Date().getTime()-datetime.getTime()
    if(period<60*60*1000){
      var t=period/(60*1000)
      datetimestr+=t.toFixed(0)+"分钟"
    }else{
      if(period<24*60*60*1000){
        var t=period/(60*60*1000)
        datetimestr+=t.toFixed(0)+"小时"
      }else{
        if(period<30*24*60*60*1000){
          var t=period/(24*60*60*1000)
          datetimestr+=t.toFixed(0)+"天"
        }else{
          if(period<12*30*24*60*60*1000){
            var t=period/(30*24*60*60*1000)
            datetimestr+=t.toFixed(0)+"月"
          }else{
            var t=period/(12*30*34*60*1000)
            datetimestr+=t.toFixed(0)+"年"
          }
        }
      }
    }
    datetimestr+="前"
    dynamics.datetime=datetimestr
      dynamicslist.push(dynamics)
    }
    this.setData({
      dynamics:dynamicslist,
      squarelast:result.content.length<5
    })
    console.log(this.data.dynamics)
    return result.last
  },
  changetabbar(e){
    this.setData({
      tabbarindex:e.currentTarget.dataset.index
    })
    if(this.data.tabbarindex==0){
      this.setData({
        shops:[],
        shoplast:false,
        shoppage:0
      })
      this.getshop()
    }
  },
  searchdynamic(e){
    console.log(e.detail.value)
    this.setData({
      squarepage:0,
      squarelast:false,
      squaresearchkey:e.detail.value,
      dynamics:[]
    })
    this.getdynamic(e.detail.value)
  },
  changesquaretabbar(e){
    this.setData({
      squarepage:0,
      squarelast:false,
      squaretabbarindex:e.currentTarget.dataset.index,
      dynamics:[]
    })
    if(this.data.squaresearchkey==""){
      this.getdynamic(null)
    }else{
      this.getdynamic(this.data.squaresearchkey)
    }
  },
  changelove:function(e){
    var index=e.currentTarget.dataset.index
    var love=e.currentTarget.dataset.love
    console.log("index",index)
    console.log("love",love)
    this.setData({
      ['dynamics['+index+'].islove']:love
    })
    console.log(this.data.dynamics)
    if(love){
      this.setData({
      ['dynamics['+index+'].lovenumber']:this.data.dynamics[index].lovenumber+1
      })
    }else{
      this.setData({
        ['dynamics['+index+'].lovenumber']:this.data.dynamics[index].lovenumber-1
        })
    }
    var id=this.data.dynamics[index].id
    var token=getApp(). globalData.accessToken
    console.log(id)
    axios.patch('/dynamics/likes?id='+id+"&like="+love)
  },
  createdynamic(e){
   wx.navigateTo({
     url:"../release/relase"
   })
  },
  showcomment(e){
    var index=e.currentTarget.dataset.index      
    var dynamicdetail=this.data.dynamics[index]
    wx.navigateTo({
      url:"../comment/comment?dynamicdetail="+JSON.stringify(dynamicdetail)
    })
  },
  getphone(e){
    var index=e.currentTarget.dataset.index
    wx.setClipboardData({
      data: this.data.dynamics[index].contact,
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  async getshop(){
    var token=getApp().globalData.accessToken
    var user=getApp().globalData.user
    var url="/shop?flag=0&pass=1&type="+this.data.shoptabbarlist[this.data.shoptabbarindex]+"&page="+this.data.shoppage+"&size=5&name="+this.data.shopsearchkey+"&school="+user.school+"&zone="+this.data.schoolregion[this.data.regionindex]
    console.log(url)
    var that=this
    var result=await axios.get(url)
      console.log(result.content)
      var shops=result.content
      var shoplist=[]
      for (let i = 0; i < shops.length; i++) {
        const item = shops[i];
        var shop={
          id:item[0],
          userId:item[1],
          school:item[2],
          zone:item[3],
          name:item[4],
          rank:item[5],
          type:item[6],
          status:item[7],
          location:item[9]
        }
        shop.condition=item[7].split(",")
        shoplist.push(shop)
      }
      that.setData({
        shoplast: shops.length<5,
        shops:shoplist
      })
      console.log(that.data.shops)
  },
  searchshop(e){
    this.setData({
      shopsearchkey:e.detail.value
    })
    this.getshop()
  },
  changeregion(e){
    this.setData({
      regionindex:e.detail.value,
      shops:[],
      shoplast:false,
      shoppage:0
    })
    this.getshop()
  },
  getshopdetail:function(e){
    var shopdetail=this.data.shops[e.currentTarget.dataset.index]
    wx.navigateTo({
      url:"../shopdetail/shopdetail?shopdetail="+JSON.stringify(shopdetail)
    })
  },
  getparentid(e){
    this.setData({
      parentid:e.currentTarget.dataset.id
    })
  },
  changeshoptabbar(e){
    var index=e.currentTarget.dataset.index
    this.setData({
      shoptabbarindex:index,
      shops:[],
      shoplast:false,
      shoppage:0,
    })
    this.getshop()
  },
 async selectindex(e){
    this.setData({
      schoolindex:e.detail.value
    })
    var token=getApp().globalData.accessToken
      var user=getApp().globalData.user
    if(this.data.squaretabbarindex==0){
      var zone=""
     }else{
       var zone=this.data.squaretabbarlist[this.data.squaretabbarindex]
     }
     var result=await axios.get('/shop?flag=0&pass=1&shool='+user.school+"&type="+this.data.squaretabbarlist[this.da
    .squaretabbarindex]+"&zone="+zone)
   console.log(result)
   var myshops=result.content
   var shopdetails=[]
   for (let i = 0; i < myshops.length; i++) {
     const myshop = myshops[i];
     var shopdetail={}
   shopdetail.title=myshop[4]
   shopdetail.rank=myshop[1]
   shopdetail.priceavg=myshop[8]
   shopdetail.address=myshop[9]
   shopdetail.require=myshop[7]
   shopdetail.type=myshop[6]
   shopdetail.id=myshop[0]
   shopdetails.push(shopdetail)
   }
   this.setData({
     shopmsg:shopdetails
   })
  },
 async gettabbarindex(e){
    this.setData({
      tabbarindex:e.currentTarget.dataset.index
    })
    if(this.data.tabbarindex==0){
      var token=getApp().globalData.accessToken
      var user=getApp().globalData.user
      this.setData({
        schoolName:user.school
      })
        var zone=this.data.squaretabbarlist[this.data.squaretabbarindex]
        var result=await axios.get("/shop?flag=0&pass=1&school="+user.school+"&zone="+zone+"&type="+this.data.squaretabbarlist[this.data.squaretabbarindex])
    console.log(result)
    var myshops=result.content
    var shopdetails=[]
    for (let i = 0; i < myshops.length; i++) {
      const myshop = myshops[i];
      var shopdetail={}
    shopdetail.title=myshop[4]
    shopdetail.rank=myshop[1]
    shopdetail.priceavg=myshop[8]
    shopdetail.address=myshop[9]
    shopdetail.require=myshop[7]
    shopdetail.type=myshop[6]
    shopdetail.id=myshop[0]
    shopdetails.push(shopdetail)
    }
    this.setData({
      shopmsg:shopdetails
    })
    console.log(this.data.shopmsg)
    }else{
      var token=getApp(). globalData.accessToken
      var result=await axios.get("/dynamics?personal=false&type="+this.data.sctabbarlist[this.data.sctabbarindex]+"&tag="+this.data.dysearchkey)
    var dynamicslist=[]
    console.log(result)
    for (let i = 0; i < result.content.length; i++) {
      const item = result.content[i];
      var dynamics={id:0,content:"",name:"",time:"",tag:[""],src:[""],commentnumber:0,lovenumber:0,islove:false}
      dynamics.id=item[0]
      dynamics.content=item[1]
      var tags=item[3].split(",")
      dynamics.tag=tags
      dynamics.anonymous=item[4]
      dynamics.likes=item[6]
      dynamics.userId=item[8]
      dynamics.contact=item[9]
      dynamics.commentnumber=item[10]
      if(item[12]==0){
        dynamics.islove=true
      }else{
        dynamics.islove=false
      }
      dynamics.commentnumber=item[11]
     var srcs=item[2].split(",")
      dynamics.src=srcs
      dynamicslist.push(dynamics)
      console.log(result)
    }
    this.setData({
      dynamics:dynamicslist
    })
    console.log(this.data.dynamics)
    }
  },
 async getsquaretabbarindex(e){
    this.setData({
      squaretabbarindex:e.currentTarget.dataset.index
    })
    var token=getApp().globalData.accessToken
      var user=getApp().globalData.user
      this.setData({
        schoolName:user.school
      })
      if(this.data.squaretabbarindex==0){
       var zone=""
      }else{
        var zone=this.data.squaretabbarlist[this.data.squaretabbarindex]
      }
      var result=await axios.get("/shop?flag=0&pass=1&school="+user.school+"&type="+this.data.squaretabbarlist[this.data.squaretabbarindex]+"&zone="+zone)
    console.log(result)
    var myshops=result.content
    var shopdetails=[]
    for (let i = 0; i < myshops.length; i++) {
      const myshop = myshops[i];
      var shopdetail={}
    shopdetail.title=myshop[4]
    shopdetail.rank=myshop[1]
    shopdetail.priceavg=myshop[8]
    shopdetail.address=myshop[9]
    shopdetail.require=myshop[7]
    shopdetail.type=myshop[6]
    shopdetail.id=myshop[0]
    shopdetails.push(shopdetail)
    }
    this.setData({
      shopmsg:shopdetails
    })
  },
 async getsctabbarindex(e){
    this.setData({
      sctabbarindex:e.currentTarget.dataset.index
    })
    var index=e.currentTarget.dataset.index
    var token=getApp(). globalData.accessToken
    var result=await axios.get("/dynamics?personal=false&type="+this.data.sctabbarlist[index])
        var dynamicslist=[]
        console.log(result)
        for (let i = 0; i < result.content.length; i++) {
          const item = result.content[i];
          var dynamics={id:0,content:"",name:"",time:"",tag:[""],src:[""],commentnumber:0,lovenumber:0,islove:false}
          dynamics.id=item[0]
          dynamics.content=item[1]
          var tags=item[3].split(",")
          dynamics.tag=tags
          dynamics.anonymous=item[4]
          dynamics.likes=item[6]
          dynamics.userId=item[8]
          dynamics.contact=item[9]
          dynamics.commentnumber=item[10]
          if(item[11]==1){
            dynamics.islove=true
          }else{
            dynamics.islove=false
          }
         var srcs=item[2].split(",")
          dynamics.src=srcs
          dynamicslist.push(dynamics)
          console.log(result)
        }
        this.setData({
          dynamics:dynamicslist
        })
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  release:function(){
    wx.navigateTo({
     url:"../release/relase"
    })
  },
  getdysearchkey(e){
    this.setData({
      dysearchkey:e.detail.value
    })
  },
 async searchdy(){
    var result=await axios.get("/dynamics?personal=false&type="+this.data.sctabbarlist[this.data.sctabbarindex]+"&tag="+this.data.dysearchkey)
    var dynamicslist=[]
    for (let i = 0; i < result.content.length; i++) {
      const item = result.content[i];
      var dynamics={id:0,content:"",name:"",time:"",tag:[""],src:[""],commentnumber:0,lovenumber:0,islove:false}
      dynamics.id=item[0]
      dynamics.content=item[1]
      var tags=item[3].split(",")
      dynamics.tag=tags
      dynamics.anonymous=item[4]
      dynamics.likes=item[6]
      dynamics.userId=item[8]
      dynamics.contact=item[9]
      dynamics.commentnumber=item[10]
      if(item[11]==1){
        dynamics.islove=true
      }else{
        dynamics.islove=false
      }
     var srcs=item[2].split(",")
      dynamics.src=srcs
      dynamicslist.push(dynamics)
      console.log(result)
    }
    this.setData({
      dynamics:dynamicslist
    })
  },
  async logininit(){
    var app=getApp()
      const {code:code}= await wx.p.login({})
      var baseUrl=axios.baseUrl
      var token=getApp().globalData.accessToken
      var res= await wx.p.request({
        url:baseUrl+"/accesstoken?code="+code,
        method:'POST',
        header:{
            'content-type': 'application/json'
        }
      })//请求模板
        var accessToken=res.data.accesstoken
        var user=res.data.user
        app.globalData.accessToken = accessToken;
        app.globalData.user=user;
         await wx.p.switchTab({
            url:"../calendar/calendar"
          })  
      
  },
  /**
   * 生命周期函数--监听页面加载
   */
  
 async onLoad() {
    var user=getApp().globalData.user
    this.setData({
      schoolname:user.school
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
 async onShow() {
    switch(this.data.tabbarindex){
      case 1:
        this.setData({
          squaresearchkey:"",
          squarepage:0,
          dynamics:[],
          squarelast:false
        })
        this.getdynamic(null)
        break
      case 0:
        break
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    switch (this.data.tabbarindex){
      case 1:
        this.setData({
          squaresearchkey:"",
          squarepage:0,
          dynamics:[],
          squarelast:false
        })
        this.getdynamic(null)
        break
      case 0:
        this.setData({
          shopsearchkey:"",
          shoppage:0,
          shops:[],
          shoplast:false
        })
        this.getshop()
        break
    }
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log(this.data.tabbarindex)
    switch(this.data.tabbarindex){
      case 1:
        console.log("触底")
        if(!this.data.ondown&&!this.data.squarelast){
          this.setData({
            squarepage:this.data.squarepage+1,
            ondown:true
          })
          if(this.data.squaresearchkey==""){
            this.getdynamic(null)
          }else{
            this.getdynamic(this.data.squaresearchkey)
          }
          this.setData({
            ondown:false
          })
        }
        break
        case 0:
          if(!this.data.shopondown&&!this.data.shoplast){
            this.setData({
              shopondown:true,
              shoppage:this.data.shoppage+1
            })
            this.getshop()
            this.setData({
              shopondown:false
            })
          }
        break
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})